<?php
/**
 * 显示用户ID和用户姓名及相关菜单的数据列.
 * User: ZFC
 * Date: 2015/8/18
 * Time: 10:31
 */

namespace backend\widgets;


use yii\grid\DataColumn;
use yii\helpers\Url;
use yii\helpers\Html;

class UserColumn extends DataColumn {


    public $label = '姓名';

    public $format = 'raw';

    public $action = 'profile';

    public $xlFormat;

    public function init()
    {
        parent::init();
    }

    public function getDataCellValue($model, $key, $index)
    {
        $user_id = parent::getDataCellValue($model, $key, $index);
        $real_name = '';
        if(isset($model->realName) && !empty($model->realName)){
            $user_id = $model->id;
            $real_name = $model->realName;
        }elseif( !empty($model->user) && !empty($model->user->realName) ){
            $user_id = $model->userID;
            $real_name = $model->user->realName;
        }
        if(!$user_id){
            return '';
        }else{
            $menu = Dropdown::widget([
                'items' => [
                    ['label'=>'查看关系', 'url'=>Url::to(['user/relation', 'id'=>$user_id])],
                    ['label'=>'更改积分', 'url'=>Url::to(['user/change-integral', 'id'=>$user_id])],
                    ['label'=>'积分记录', 'url'=>Url::to(['user/integral-record', 'id'=>$user_id])],
                    ['label'=>'投资记录', 'url'=>Url::to(['user/invest-record', 'id'=>$user_id])],
                    ['label'=>'充值记录', 'url'=>Url::to(['user/charge-record', 'id'=>$user_id])],
                    ['label'=>'提现记录', 'url'=>Url::to(['user/cash-record', 'id'=>$user_id])],
                    ['label'=>'借款记录', 'url'=>Url::to(['user/loan-record', 'id'=>$user_id])],
                    ['label'=>'还款记录', 'url'=>Url::to(['user/repayment-record', 'id'=>$user_id])],
                    ['label'=>'加入黑名单', 'url'=>Url::to(['black-white/blacklist-add', 'userID'=>$user_id])],
                    ['label'=>'加入白名单', 'url'=>Url::to(['black-white/whitelist-add', 'userID'=>$user_id])],
                ],
            ]);
            if(!$real_name) $real_name = $user_id;
            return '<div class="btn-group">
                      <a href="'.Url::to(['user/profile', 'id'=>$user_id]).'" class="btn btn-xs btn-flat btn-warning" style="min-width: 50px; text-align:left;">'.$real_name.'</a>
                      <button type="button" class="btn btn-xs btn-flat btn-warning dropdown-toggle" data-toggle="dropdown">
                        <span class="caret"></span>
                        <span class="sr-only">下拉</span>
                      </button>
                        '.$menu.'
                    </div>';
        }
    }

    public function renderPageSummaryCell(){
        return false;
    }
}